package com.yellowpage.ca.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.ca.data.RoleFunction;
import com.yellowpage.ca.dao.RoleFunctionDao;

public class RoleFunctionServiceImpl extends BaseServiceImpl implements
		RoleFunctionService {
	private RoleFunctionDao roleFunctionDao;

	public void setRoleFunctionDao(RoleFunctionDao roleFunctionDao) {
		this.roleFunctionDao = roleFunctionDao;
	}

	/* Smooth generate Method */
	public RoleFunction getRoleFunction(Long id) {
		return roleFunctionDao.get(id);
	}

	public List<RoleFunction> getAllRoleFunction() {
		return roleFunctionDao.getAll();
	}

	public List<Map> queryRoleFunctionByPage(Map queryMap, PageRender pageRender) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						"SELECT a.ROLE_FUNCTION_ID , a.ROLE_ID , a.FUNCTION_ID , "
								+ "       (SELECT NAME FROM CA_FUNCTION where FUNCTION_ID = a.FUNCTION_ID) as FUNC_NAME, "
								+ "       (SELECT NAME FROM CA_ROLE where ROLE_ID = a.ROLE_ID) as ROLE_NAME, "
								+ "       (select fm.NAME from CA_FUNC_MODULE fm, CA_FUNCTION  fu where fm.FUNC_MODULE_ID = fu.FUNC_MODULE_ID and fu.FUNCTION_ID = a.FUNCTION_ID) as FUNC_MODULE,"
								+ "       (select DISPLAY_NAME from CA_USER where USER_ID = a.CREATE_USER) as CREATE_USER,"
								+ "       date_format(a.CREATE_TIME,'%Y-%m-%d %T') as CREATE_TIME   FROM CA_ROLE_FUNCTION a WHERE 1=1 ")
				.addCondition(" and ROLE_FUNCTION_ID = :roleFunctionId ")
				.addCondition(" and ROLE_ID = :roleId ")
				.addCondition(" and FUNCTION_ID = :functionId ")
				.addCondition(" and CREATE_USER = :createUser ")
				.addCondition(
						" and date_format(CREATE_TIME,'%Y-%m-%d') =  :createTime  ");

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	public Map getRoleMap() {
		Map roleMap = new LinkedMap();

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("  select NAME, r.ROLE_ID as VALUE from CA_ROLE r ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());

		for (int i = 0; i < resultList.size(); i++) {
			roleMap.put(resultList.get(i).get("VALUE"), resultList.get(i)
					.get("NAME"));
		}

		return roleMap;

	}

	public Map getFunctionMap() {
		Map functionMap = new LinkedMap();

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("  select CONCAT('(', fm.NAME, ') ', f.NAME) as NAME , f.FUNCTION_ID as VALUE " +
						"  from CA_FUNCTION f, CA_FUNC_MODULE fm " +
						" where f.FUNC_MODULE_ID = fm.FUNC_MODULE_ID order by fm.NAME ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());

		for (int i = 0; i < resultList.size(); i++) {
			functionMap.put(resultList.get(i).get("VALUE"), resultList.get(i)
					.get("NAME"));
		}

		return functionMap;

	}

	public void saveRoleFunction(RoleFunction roleFunction) {
		roleFunctionDao.save(roleFunction);
	}

	public void updateRoleFunction(RoleFunction roleFunction) {
		roleFunctionDao.update(roleFunction);
	}

	public void delete(List deleteList) {
		roleFunctionDao.remove(deleteList);
	}

}